﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tail4Net.Properties;
using Microsoft.Win32;
using System.Runtime.InteropServices;

namespace LogViewer
{
    public partial class OptionsForm : Form
    {
        char[] seperator = { ';' };

        public char[] Seperator
        {
            get
            {
                return seperator;
            }
        }

        
        public bool IsFilterEnabled
        {
            get
            {
                return chkFilter.Checked && !String.IsNullOrEmpty(FilterText);
            }
            set
            {
                chkFilter.Checked = value;
            }
        }

        public String FilterText
        {
            get
            {
                return chkFilter.Checked? ddlFilterText.Text: null;
            }
            set
            {
                ddlFilterText.Text = value;
            }
        }

        public Color FilterColor
        {
            get
            {
                return cmdColor.BackColor;
            }
            set
            {
                cmdColor.BackColor = clrFilter.Color = value;
            }
        }

        public OptionsForm()
        {
            InitializeComponent();

            Tail4NetConf conf = Tail4NetConf.Default;

            clrFilter.Color = cmdColor.BackColor = conf.FilterColor;
            chkFilter.Checked = conf.FilterEnabled;
            ddlFilterText.Text = conf.FilterText;
        }

        private void cmdColor_Click(object sender, EventArgs e)
        {
            clrFilter.ShowDialog(this.Owner);

            cmdColor.BackColor = clrFilter.Color;
        }

        private void cmdOk_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            Hide();
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Hide();
        }

        private void chkFilter_CheckedChanged(object sender, EventArgs e)
        {
            ddlFilterText.Enabled = cmdColor.Enabled = chkFilter.Checked;
        }

        public void AssociateExt(bool isYes)
        {
            if (isYes)
            {
                SetAssociation(".log", "Log_File", Application.ExecutablePath, "Application Log File");
                MessageBox.Show("Log files will now be opened with tail4net on double-clicking .log files in Windows Explorer", "tail4net", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                RemoveAssociation(".log", "Log_File", Application.ExecutablePath, "Application Log File");
                MessageBox.Show("Log files disassociated from tail4net", "tail4net", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        public static void SetAssociation(string Extension, string KeyName, string OpenWith, string FileDescription)
        {
            RegistryKey BaseKey;
            RegistryKey OpenMethod;
            RegistryKey Shell;
            RegistryKey CurrentUser;

            BaseKey = Registry.ClassesRoot.CreateSubKey(Extension);
            BaseKey.SetValue("", KeyName);

            OpenMethod = Registry.ClassesRoot.CreateSubKey(KeyName);
            OpenMethod.SetValue("", FileDescription);
            OpenMethod.CreateSubKey("DefaultIcon").SetValue("", "\"" + OpenWith + "\",0");
            Shell = OpenMethod.CreateSubKey("Shell");
            //Shell.CreateSubKey("edit").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\"");
            Shell.CreateSubKey("open").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\"");
            BaseKey.Close();
            OpenMethod.Close();
            Shell.Close();

            // Delete the key instead of trying to change it
            //CurrentUser = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + Extension, true);
            //CurrentUser.DeleteSubKey("UserChoice", false);
            //CurrentUser.Close();

            // Tell explorer the file association has been changed
            SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero);

        }

        public static void RemoveAssociation(string extension, string keyName, string openWith, string fileDescription)
        {
            RegistryKey baseKey;
            RegistryKey openMethod;
            RegistryKey shell;
            RegistryKey currentUser;

            baseKey = Registry.ClassesRoot.CreateSubKey(extension);
            baseKey.SetValue("", keyName);

            openMethod = Registry.ClassesRoot.CreateSubKey(keyName);
            openMethod.SetValue("", fileDescription);

            //openMethod.DeleteSubKey("DefaultIcon", false);
            openMethod.CreateSubKey("DefaultIcon").SetValue("", "\"" + "notepad.exe" + "\",0");
            shell = openMethod.CreateSubKey("Shell");
            //Shell.CreateSubKey("edit").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\"");
            shell.CreateSubKey("open").CreateSubKey("command").SetValue("", "\"" + "notepad.exe" + "\"" + " \"%1\"");
            baseKey.Close();
            openMethod.Close();
            shell.Close();

            // Delete the key instead of trying to change it
            //currentUser = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + extension, true);
            //currentUser.DeleteSubKey("UserChoice", false);
            //currentUser.Close();

            // Tell explorer the file association has been changed
            SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero);

        }

        [DllImport("shell32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);

        private void cmdAssociateFiles_Click(object sender, EventArgs e)
        {
            AssociateExt(true);
        }

        private void cmdUnassociate_Click(object sender, EventArgs e)
        {
            AssociateExt(false);
        }

        private void OptionsForm_Load(object sender, EventArgs e)
        {
            ddlFilterText.Enabled = cmdColor.Enabled = chkFilter.Checked;
        }

    }
}
